﻿#ifndef __REDISDB_QUERY_TASK_HEAD__
#define __REDISDB_QUERY_TASK_HEAD__

#include <string>
#include <vector>
#include <functional>
#include "multi_task/task.h"
#include "redis_query_result.h"

class CRedisDbQueryTask : public MultiTask::TaskBase
{
public:
	CRedisDbQueryTask(const std::string &dbName, const std::string &collectionName, const std::string &queryString = "{}", const std::string &fieldsString = "");
	~CRedisDbQueryTask();

	using QueryCBType = std::function<void(const RedisQueryResultSptr &)>;
	std::shared_ptr<MultiTask::TaskBase> SetQueryCB(const QueryCBType & cb)
	{
		m_cb = cb;
		return shared_from_this();
	}

public:
	const char *GetName() override
	{
		return m_taskInstName.c_str();
	}
	void Process(void *sharedObj) override;
	void OnCompleted() override;
	void *CreateSharedObj(unsigned index) override;

private:
	std::string m_instName;
	std::string m_dbName;
	std::string m_collectionName;
	std::string m_queryString;
	std::string m_fieldsString;
	RedisQueryResultSptr m_resultSptr;
	QueryCBType m_cb;
	std::string m_taskInstName;
};

#define RedisQuery(...) std::make_shared<CRedisDbQueryTask>(__VA_ARGS__)

#endif  //__REDISDB_QUERY_TASK_HEAD__
